package com.hyyt.shopjsp.basic.controller;

import com.hyyt.shopjsp.basic.pojo.BasicPurview;
import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorService;
import com.hyyt.shopjsp.basic.service.IBasicUsersService;
import com.hyyt.shopjsp.util.redis.service.IRedisBasicService;
import com.hyyt.shopjsp.common.utils.PurviewUtil;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.*;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/21 0021上午 10:27.
 */
@Controller
@RequestMapping("/back/workspace")
public class WorkspaceController extends BaseController {
    private Logger logger = Logger.getLogger(WorkspaceController.class);
    private String basePath = "/back/workspace/";

    @Resource
    private IBasicUsersService basicUsersService;
    @Resource
    private IBasicActorService basicActorService;
    @Resource
    private IRedisBasicService redisBasicService;

    /**
     * 登录后进入首页
     * @return
     */
    @RequestMapping("index")
    public String index(Integer purviewId, Map<String, Object> map) {
        //验证用户权限
        String userName = getSessionUsersName();
        BasicUsers users = getSessionUsers();
        String userPhoto = users.getUserPhoto();

        List<BasicPurview> basicPurviews = new ArrayList<>();
        String purview = RedisDataReadUtil.getRedisValue(StringStaticKey.PURVIEW_USER_PREFIX + userName);
        basicPurviews = FastJsonUtils.toList(purview, BasicPurview.class);

        //获取左侧二，三级菜单
        if(Utils.objectIsEmpty(purviewId)){
            List<Integer> purviewIds = getPurviewIds(basicPurviews);
            //判断当权限id集合不为空
            if(Utils.objectIsNotEmpty(purviewIds)){
                purviewId= Collections.min(purviewIds);//获取所有一级权限中最小的那个id
            }
        }
        List<BasicPurview> purviews=leftPurview(purviewId);
        map.put("purviews",purviews);

        //设置管理员名称
        map.put("userName",userName);
        map.put("userPhoto",userPhoto);
        //获取一级菜单权限
        map.put("basicPurviews", PurviewUtil.getBasicPurviewByLeaf(basicPurviews));
        //设置以及菜单被选中的
        map.put("checkedPurviewId",purviewId);
        return basePath + "workspace";
    }

    /**
     * 动态获取左侧菜单
     * @param purviewId
     * @return
     */

    public List<BasicPurview> leftPurview(Integer purviewId/*,HttpServletResponse response*/) {
        Map<String, Object> map=new HashMap<String, Object>();
        String userName = getSessionUsersName();
        List<BasicPurview> allBasicPurviews, basicPurviews, addParentPurviews = new ArrayList<>();
        String purview = RedisDataReadUtil.getRedisValue(StringStaticKey.PURVIEW_USER_PREFIX + userName);
        allBasicPurviews = FastJsonUtils.toList(purview, BasicPurview.class);
        //获得当前用户的权限下，所拥有的操作权限集合
        basicPurviews = PurviewUtil.getBasicPurviewByLeaf(allBasicPurviews, purviewId);

        boolean purviewIsLeaf = false;  //默认不是子结点

        //判断第一级操作权限树是否是子结点
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview basicPurview : basicPurviews) {
                Integer isLeaf = basicPurview.getIsLeaf();
                //判断 如果是子节点
                if (Utils.objectIsNotEmpty(isLeaf) && isLeaf.compareTo(1) == 0) {
                    purviewIsLeaf = true;
                }
            }
        }
        //如果是，则添加总导航为父结点以做显示所用
        if (purviewIsLeaf) {
            BasicPurview addParentPurview = PurviewUtil.getBasicPurviewById(allBasicPurviews, purviewId);
            addParentPurview.setBasicPurviews(basicPurviews);
            addParentPurviews.add(addParentPurview);
            return  addParentPurviews;
        } else {
            return  basicPurviews;
        }
    }

    /**
     * 进入欢迎页面
     * @return
     */
    @RequestMapping("welcome")
    public String welcome() {
        return basePath + "welcome";
    }

    /**
     * 获得权限id集合
     * @param basicPurviews
     * @return
     */
    private List<Integer> getPurviewIds(List<BasicPurview> basicPurviews) {
        if (Utils.objectIsEmpty(basicPurviews) || basicPurviews.size() == 0) return null;
        List<Integer> list = new ArrayList<>();
        for (BasicPurview purview : basicPurviews) {
            list.add(purview.getPurviewId());
        }
        return list;
    }
}